package com.service.yuejuan;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.model.*;
import com.utils.ConstData;

import java.util.Iterator;
import java.util.List;

/**
 * Created by john on 2017/8/1.
 */
public class YuejuanService {
    private PaperConfig paperConfig=new PaperConfig();
    private Judge judge=new Judge();

    public List<PaperConfig>  getAllPaperConfig(){
        return paperConfig.dao().find("select * from paperconfig");
    }

    public List<Student> getStudentBySclass(String Sclass){
        return Student.dao.find("select student.* from student , studentexam where" +
                " student.sno=studentexam.sno and starttime is not null and student.sclass = '"+ Sclass + "'");
    }

    public  List<Student> getAllStudent(){
        return  Student.dao.find("select student.* from student , studentexam where" +
                " student.sno=studentexam.sno and starttime is not null");
    }

    public List<StudentPaper> DoneOneStudent(String sno,
                                             List<PaperConfig> paperConfigList) {
        List<StudentPaper> studentPaper = StudentPaper.dao.find("select StudentPaper.* from StudentPaper," +
                "paperconfig  where StudentPaper.paperconfig_id=paperconfig.id and sno='"+sno+"'");
        try {
            Judge.dao.systemYuejuan(studentPaper);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return studentPaper;
    }

    @SuppressWarnings({ "unchecked" })
    public double DoneOneScore(String sno, PaperConfig paperConfig) {
        double allScore = 0;
        try {
            double choice = 0;
            double simpleFill = 0;
            double complexFill = 0;
            double judge1 = 0;
            double simplePress = 0;
            double gongwenxiezuo = 0;
            double choiceFill = 0;
            double choiceread = 0;
            // 得到确定学生，确定科目的所有题目
            List<StudentPaper> studentPaperList = Judge.dao.getStudentPaper(sno,
                    Integer.parseInt(String.valueOf(paperConfig.get("id"))));
            if (null != studentPaperList && (0 != studentPaperList.size())) {
                // 转化成迭代器
                Iterator<StudentPaper> studentPaper_it = studentPaperList
                        .iterator();
                while (studentPaper_it.hasNext()) {
                    StudentPaper studentPaper = (StudentPaper) studentPaper_it
                            .next();
                    if (!studentPaper.get("isright") .equals(ConstData.WRONG)
                            || !studentPaper.get("type").equals(ConstData.COMPLEX_FILL) ) {
                        if (studentPaper.get("type").equals(ConstData.CHOICE)) {
                            choice += judge.getOneScore("CHOICE",
                                    studentPaper.get("ques_id") + "");
                        } else if (studentPaper.get("type").equals(ConstData.SIMPLE_FILL)) {
                            simpleFill += judge.getOneScore("SIMPLEFILL",
                                    studentPaper.get("ques_id") + "");
                        } else if (studentPaper.get("type").equals(ConstData.COMPLEX_FILL)) {
                            complexFill += judge.getCFScore("COMPLEXFILL",
                                    studentPaper);
                        } else if (studentPaper.get("type") .equals (ConstData.JUDGE)) {
                            judge1 += judge.getOneScore("JUDGE",
                                    studentPaper.get("ques_id") + "");
                        } else if (studentPaper.get("type") .equals (ConstData.SIMPlE_PRESS)) {
                            simplePress += judge.getOneScore("SIMPlEPRESS",
                                    studentPaper.get("ques_id") + "");
                        } else if (studentPaper.get("type") .equals (ConstData.GONGWENXIEZUO)) {
                            gongwenxiezuo += judge.getOneScore(
                                    "GONGWENXIEZUO",
                                    studentPaper.get("ques_id") + "");
                        } else if (studentPaper.get("type") .equals (ConstData.CHOICE_FILL)) {
                            choiceFill += judge.getOneScore("CHOICEFILL",
                                    studentPaper.get("ques_id") + "");
                        } else if (studentPaper.get("type") .equals (ConstData.CHOICEREAD)) {
                            choiceread += judge.getOneScore("CHOICEREAD",
                                    studentPaper.get("ques_id") + "");
                        }
                    }
                }
                Score score = null;
                if (judge.loadScore1(sno,Integer.parseInt(String.valueOf(paperConfig.get("course_id")))) != null) {
                    score = judge.loadScore1(sno,Integer.parseInt(String.valueOf(paperConfig.get("course_id"))));
                } else {
                    score = new Score();
                }

                allScore = choice + simpleFill + complexFill + judge1
                        + simplePress + gongwenxiezuo + choiceFill + choiceread;

                score.set("stuno",sno);
                score.set("course",paperConfig.getCourse());
                score.set("choice",choice);
                score.set("simplefill",simpleFill);
                score.set("complex",complexFill);
                score.set("judge",judge1);
                score.set("simplePress",simplePress);
                score.set("choiceFill",choiceFill);
                score.set("choiceRead",choiceread);
                score.set("Allscore",allScore);
                score.set("ispublic",0);
                score.save();
            } else {
                return -1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return allScore;
    }

    /**
    *@Author:HeXun
    *@Description:
    *@Date: 2017/8/4 0004
    */
    public int markCount(int courseId){
        Record record= Db.findFirst("select count(*) count from studentpaper,paperconfig,courses where paperconfig.course_id = courses.id and courses.id=? and studentpaper.paperconfig_id=paperconfig.id and studentpaper.isMarked=1",courseId);
        int num=Integer.parseInt(record.get("count").toString());
        if(num>0){
            return num;
        }
        return -100;
    }

    /**
    *@Author:HeXun
    *@Description:
    *@Date: 2017/8/4 0004
    */
    public int courseScore(int courseId){
        Object obj=Score.dao.findFirst("select isPublic from score where course = ?",courseId);
        int i=obj==null?0:Integer.parseInt((String) obj);
        return i;
    }

    /**
    *@Author:HeXun
    *@Description: 更改成绩发布状态
    *@Date: 2017/8/4 0004
    */
    public boolean updataStatus(String course_id,String status){
        int i=Db.update("update score set ispublic =? where course = ?",status,course_id);
        if(i>0){
            return true;
        }
        return false;
    }
}
